cmake_minimum_required(VERSION 3.10)
project(SplineTrajectoryExamples)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Eigen3
find_package(Eigen3 REQUIRED)

# Fallback for systems without proper Eigen3 CMake config
if(NOT TARGET Eigen3::Eigen)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(EIGEN3 REQUIRED eigen3)
    include_directories(${EIGEN3_INCLUDE_DIRS})
endif()

# Include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Set build type if not specified
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Performance test executables
add_executable(test_cubic_spline_vs_minco_nd test_cubic_spline_vs_minco_nd.cpp)
target_link_libraries(test_cubic_spline_vs_minco_nd Eigen3::Eigen)

add_executable(test_quintic_spline_vs_minco_nd test_quintic_spline_vs_minco_nd.cpp)
target_link_libraries(test_quintic_spline_vs_minco_nd Eigen3::Eigen)

# Example executables
add_executable(basic_cubic_spline examples/basic_cubic_spline.cpp)
target_link_libraries(basic_cubic_spline Eigen3::Eigen)

add_executable(quintic_spline_comparison examples/quintic_spline_comparison.cpp)
target_link_libraries(quintic_spline_comparison Eigen3::Eigen)

add_executable(robot_trajectory_planning examples/robot_trajectory_planning.cpp)
target_link_libraries(robot_trajectory_planning Eigen3::Eigen)


# Compiler flags
set(COMMON_COMPILE_FLAGS -Wall -Wextra)
set(DEBUG_FLAGS -g -O0)
set(RELEASE_FLAGS -O3 -march=native -ffast-math -DNDEBUG)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_options(test_cubic_spline_vs_minco_nd PRIVATE ${COMMON_COMPILE_FLAGS} ${DEBUG_FLAGS})
    target_compile_options(test_quintic_spline_vs_minco_nd PRIVATE ${COMMON_COMPILE_FLAGS} ${DEBUG_FLAGS})
    target_compile_options(basic_cubic_spline PRIVATE ${COMMON_COMPILE_FLAGS} ${DEBUG_FLAGS})
    target_compile_options(quintic_spline_comparison PRIVATE ${COMMON_COMPILE_FLAGS} ${DEBUG_FLAGS})
    target_compile_options(robot_trajectory_planning PRIVATE ${COMMON_COMPILE_FLAGS} ${DEBUG_FLAGS})
else()
    target_compile_options(test_cubic_spline_vs_minco_nd PRIVATE ${COMMON_COMPILE_FLAGS} ${RELEASE_FLAGS})
    target_compile_options(test_quintic_spline_vs_minco_nd PRIVATE ${COMMON_COMPILE_FLAGS} ${RELEASE_FLAGS})
    target_compile_options(basic_cubic_spline PRIVATE ${COMMON_COMPILE_FLAGS} ${RELEASE_FLAGS})
    target_compile_options(quintic_spline_comparison PRIVATE ${COMMON_COMPILE_FLAGS} ${RELEASE_FLAGS})
    target_compile_options(robot_trajectory_planning PRIVATE ${COMMON_COMPILE_FLAGS} ${RELEASE_FLAGS})
endif()

# Add chrono library for performance measurements
if(NOT WIN32)
    target_link_libraries(test_cubic_spline_vs_minco_nd pthread)
    target_link_libraries(test_quintic_spline_vs_minco_nd pthread)
    target_link_libraries(quintic_spline_comparison pthread)
endif()

# Create custom targets for easy building
add_custom_target(tests
    DEPENDS test_cubic_spline_vs_minco_nd test_quintic_spline_vs_minco_nd
    COMMENT "Building performance tests"
)

add_custom_target(examples
    DEPENDS basic_cubic_spline quintic_spline_comparison robot_trajectory_planning 
    COMMENT "Building examples"
)

add_custom_target(all_targets
    DEPENDS tests examples
    COMMENT "Building all targets"
)

# Status messages
message(STATUS "构建类型: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++标准: ${CMAKE_CXX_STANDARD}")
message(STATUS "项目名称: ${PROJECT_NAME}")
message(STATUS "可执行文件:")
message(STATUS "  - Performance Tests:")
message(STATUS "    * test_cubic_spline_vs_minco_nd")
message(STATUS "    * test_quintic_spline_vs_minco_nd")
message(STATUS "  - Examples:")
message(STATUS "    * basic_cubic_spline")
message(STATUS "    * quintic_spline_comparison") 
message(STATUS "    * robot_trajectory_planning")
message(STATUS "")
message(STATUS "构建命令:")
message(STATUS "  make tests     - 构建性能测试")
message(STATUS "  make examples  - 构建示例程序")
message(STATUS "  make all       - 构建所有目标")